<?php
class Transaction extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

	}
	public function index()
	{
		$data = get_initial_data($this);
		$this->load->model('transaction_model');
		$data['result'] = $this->transaction_model->get_transaction(1);
		$data['topsale'] = $this->transaction_model->gettopsale(20);
		$data['title'] = 'CG3002';
		$data['transaction'] = true;
		$data['barcode'] = false;
		$data['search'] = '';
		$this->load->view('templates/header', $data);
		$this->load->view('transaction', $data);
		$this->load->view('templates/footer', $data);
	}

	public function view($transaction_id)
	{
		if ($transaction_id){
			$data = get_initial_data($this);
			$this->load->model('transaction_model');
			$search = $this->input->get('search');
			$data['search'] = $search;
			$per_page = 30;
			$page = $this->input->get('per_page');;
			$config = array();
			$config["base_url"] = site_url() . '/transaction/view/'.$transaction_id.'?search='.$search;
			$config["total_rows"] = $this->transaction_model->count_search($transaction_id, $search);
			$config["per_page"] = $per_page;
			$config["uri_segment"] = 3;
			$config['page_query_string'] = TRUE;
			$config['use_page_numbers'] = TRUE;
			$this->pagination->initialize($config);
			$data["links"] = $this->pagination->create_links();
			$data['result'] = $this->transaction_model->search($transaction_id, $search, $per_page, $page);
			//$data['result'] = $this->transaction_model->get_transaction_detail_by_submit_id($transaction_id);
			$data['info'] = $this->transaction_model->get_transaction_by_id($transaction_id);
			$data['title'] = 'CG3002';
			$this->load->view('templates/header', $data);
			$this->load->view('transaction_detail', $data);
			$this->load->view('templates/footer', $data);
		}else{
			redirect('');
		}
	}
	
	public function search()
	{
		$data = get_initial_data($this);
		$this->load->model('transaction_model');
		$search = $this->input->get('search');
		$search_type = $this->input->get('search_type');
	
		$data['search'] = $search;
		$per_page = 30;
		$page = $this->input->get('per_page');;
		$config = array();
		$config["base_url"] = site_url() . '/transaction/search?search='.$search.'&search_type='.$search_type;
		$config["total_rows"] = $this->transaction_model->count_search_barcode($search_type, $search);
		$config["per_page"] = $per_page;
		$config["uri_segment"] = 3;
		$config['page_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		$data["links"] = $this->pagination->create_links();
		$data['result'] = $this->transaction_model->search_barcode($search_type, $search, $per_page, $page);
		//$data['result'] = $this->transaction_model->get_transaction_detail_by_submit_id($transaction_id);
		$data['title'] = 'CG3002';
		$data['search_type'] = $search_type;
		$this->load->view('templates/header', $data);
		$this->load->view('transaction_search', $data);
		$this->load->view('templates/footer', $data);
	}

	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}
}